<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Spatie\Permission\Models\Permission as PermissionModel;

class Permission extends PermissionModel
{
    protected $table = "permissions";

    protected $fillable = [
        'name', 'display_name', 'guard_name', 'path', 'level', 'hidden', 'is_menu', 'sort', 'pid', 'meta', 'component',
        'meta->title', 'meta->icon'
    ];
    protected $casts = [
        'hidden' => "boolean",
        'is_menu' => 'boolean',
        'meta' => "array"
    ];

    public static function boot()
    {
        parent::boot();
    }

    //获取菜单的子级
    public function children()
    {
        return $this->hasMany(self::class, 'pid');
    }

    //获取菜单的父级
    public function parent()
    {
        return $this->belongsTo(self::class, 'pid');
    }
    public function getMetaAttribute($value)
    {
        return ['title'=>$this->display_name,'icon'=>'dashboard'];
    }

    public function getPathAttribute($val)
    {
        return '/'.$this->name;
    }
}
